<?php
/**
 * Created by 784855684@qq.com.
 * Link: https://github.com/lichtung/wshore
 * User: linzh
 * Date: 6/30/17
 * Time: 2:55 PM
 */
declare(strict_types=1);


namespace application\module\admin\controller;

use application\module\admin\addon\log\model\LogModel;
use application\module\admin\addon\Menu;
use application\module\admin\addon\Rbca;
use application\module\admin\addon\Sign;
use application\module\admin\addon\sign\model\UserModel;
use wshore\core\Response;
use wshore\core\view\Render;
use wshore\Exception;
use wshore\helper\Base64x;
use wshore\throws\DatabaseException;

/**
 * Class admin 后台抽象类
 * @package application\module\admin\controller
 */
abstract class admin
{
    use Render;
    /**
     * 登录的账户
     * @var UserModel
     */
    protected static $user = null;

    /**
     * admin constructor.
     * 构造时检查登录和权限
     */
    public function __construct()
    {
        if (!self::$user) {
            //登陆判断
            try {
                self::$user = Sign::getInstance()->getUser();
            } catch (Exception $exception) {
            }
        }

        if (!self::$user) {
            if (WS_IS_AJAX) {
                Response::failure('no sign', [], 10);
            } else {
                //记录来源页面
                Response::redirect(WS_PUBLIC_URL . '/admin/sign/in?refer=' .
                    urlencode(Base64x::encode($_SERVER['REQUEST_URI']??'',
                        Sign::getInstance()->encodeKey)));
            }
        } else {
            $uid = (int)self::$user->id;
            if (1 !== $uid) { # 1 为默认管理员
                if (!Rbca::getInstance()->checkAuthorize($uid, WS_REQUEST_PATH)) {
                    if (WS_IS_AJAX) {
                        Response::failure('no permission', [], 11);
                    } else {
                        header('Location:' . WS_PUBLIC_URL . '/admin/index/showunauthorized');
                    }
                    die;
                }
            }
        }
    }

    /**
     * 获取客户经理ID
     *
     * 如果是跳转的链接，则需要先获取manager
     *
     * @return int
     */
    protected function getManagerId(): int
    {
        return self::$user ? (int)self::$user->id : 0;
    }

    /**
     * 记录日志
     * @param string $action
     * @param string $content
     * @return void
     * @throws DatabaseException
     */
    protected function log(string $action, string $content)
    {
        static $logar = null;
        if (!$logar) {
            $logar = new LogModel($this->getManagerId());
        }
        $logar->action = $action;
        $logar->content = $content;
        if (!$logar->save(true)) {
            throw new DatabaseException($logar->error());
        }
    }


    /**
     * 布局分配
     */
    protected function doLayoutAssign()
    {
        $sidemenu = Menu::getInstance()->getlist();
        $sign = Sign::getInstance();
        $user = $sign->getUser();
        $this->assign([
            'avatar' => $user->avatar,
            'client_name' => '',
            'nickname' => $user->nickname,
            'username' => $user->username,
            'logo_mini' => 'LITE',
            'logo' => 'LITERA ADMIN',
            'sidebarMenu' => $sidemenu,
            'socket_server' => '',
        ]);
    }

}